package com.smec.remes.module.register.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.smec.moblie.R;
import com.smec.moblie.databinding.ActivityContractBinding;
import com.smec.remes.base.SmecBaseActivity;
import com.smec.remes.module.register.SmecRegisterPresenter;
import com.smec.remes.module.register.fragment.SmecContractCompanyFragment;
import com.smec.remes.module.register.fragment.SmecContractStationFragment;
import com.smec.remes.module.register.fragment.SmecMaintain;
import com.smec.remes.util.TopBarLayout;
import com.zyyoona7.lib.EasyPopup;
import com.zyyoona7.lib.HorizontalGravity;
import com.zyyoona7.lib.VerticalGravity;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/9.
 */

public class SmecContractActivity extends SmecBaseActivity<SmecRegisterPresenter> {

    private ActivityContractBinding activityContractBinding;
    private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
    private int currentPage=0;
    private EasyPopup mCirclePop;
    private String prompt="当前客户所属维保站，所有有效保养合同的合同号梯号";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityContractBinding=DataBindingUtil.setContentView(this,R.layout.activity_contract);
        activityContractBinding.setPage(currentPage);
        _return();
        _init();
        _edit();
        _prompt();
    }

    public void _init(){
        fragmentArrayList.add(SmecMaintain.newInstance());
        fragmentArrayList.add(SmecContractStationFragment.newInstance());
        fragmentArrayList.add(SmecContractCompanyFragment.newInstance());
        activityContractBinding.viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentArrayList.size();
            }
        });
        activityContractBinding.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage=position;
                activityContractBinding.setPage(currentPage);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        activityContractBinding.contractTopbar.gettvTextRightSecond().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmecContractActivity.this.finish();
                mRxBus.post("ContractEdit","Edit");
            }
        });
    }

    public void btnclick(View view){
        switch (view.getId()){
            case R.id.by:
                currentPage=0;
                break;
            case R.id.btn_station:
                currentPage=1;
                break;
            case R.id.btn_company:
                currentPage=2;
                break;
        }
        activityContractBinding.setPage(currentPage);
        activityContractBinding.viewPager.setCurrentItem(currentPage);
    }

    public void _return(){
        activityContractBinding.contractTopbar.gettvTextRightSecond().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmecContractActivity.this.finish();
            }
        });
        activityContractBinding.contractTopbar.getTvTextLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmecContractActivity.this.finish();
            }
        });
    }

    public void _edit(){

        activityContractBinding.contractTopbar.setTopBarListener(new TopBarLayout.TopBarListener() {
            @Override
            public void setOnLeftClickListener() {
                finish();
            }

            @Override
            public void setOnRight1ClickListener() {

            }

            @Override
            public void setOnRight2ClickListener() {
                SmecContractActivity.this.finish();
                mRxBus.post("ContractEdit","edit");
            }
        });

    }

    public void _prompt(){
        activityContractBinding.ivPrompt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(currentPage==0){
                    prompt="当前客户所属维保站，所有有效保养合同的合同号梯号" +
                            "在保养合同有效期内，以及保养合同到期后三个月内的电梯为有效保养合同";
                }else {
                    prompt="当前客户所属分公司，所有有效保养合同的合同号梯号" +
                            "在保养合同有效期内，以及保养合同到期后三个月内的电梯为有效保养合同";
                }
                _getPopuWindow(activityContractBinding.ivPrompt);
            }
        });
    }

    private void _getPopuWindow(View view){
        mCirclePop=new EasyPopup(this)
                .setContentView(R.layout.layout_prompt)
                .setAnimationStyle(R.style.QQPopAnim)
                //是否允许点击PopupWindow之外的地方消失
                .setFocusAndOutsideEnable(true)
                .setWidth(900)
                .setHeight(290)
                //指定任意 ViewGroup 背景变暗
                .createPopup();

        mCirclePop.showAtAnchorView(view, VerticalGravity.CENTER, HorizontalGravity.LEFT, 0, 0);

        TextView smecCompare=mCirclePop.getView(R.id.tv_prompt);
        smecCompare.setText(prompt);
    }

    @Override
    public SmecRegisterPresenter getPresenter() {
        return new SmecRegisterPresenter(this);
    }
}
